<template>
	<view>
		<scroll-view scroll-y class="page">
			<!-- 头部 -->
			<cu-custom bgColor="bg-rgb" :isBack="true">
				<block slot="backText">返回</block>
				<block slot="content">工单备料项目</block>
				<view slot="right" @tap="rightClick">凭证抬头</view>
			</cu-custom>
			<!-- 抬头凭证 输入弹窗 -->
			<view class="cu-modal" :class="modalName=='Modal'?'show':''">
				<view class="cu-dialog">
					<view class="cu-bar bg-white justify-end">
						<view class="content">凭证抬头</view>
						<view class="action">
							<text class="cuIcon-close text-red" @click="close"></text>
						</view>
					</view>
					<view class="padding-xl">
						<form>
							<view class="cu-form-group">
								<view class="titles">凭证日期：</view>
								<picker mode="date" :value="myModelData.cerDate" @change="cerDateChange">
									<view class="picker">
										{{myModelData.cerDate}}
									</view>
								</picker>
							</view>
							<view class="cu-form-group">
								<view class="titles">过账日期：</view>
								<picker mode="date" :value="myModelData.accountDate" @change="accountDateChange">
									<view class="picker">
										{{myModelData.accountDate}}
									</view>
								</picker>
							</view>
							<view class="cu-form-group">
								<view class="titls">类型：</view>
								<input placeholder="工号" style="text-align: right;" name="input" v-model="myModelData.affairType" disabled></input>
							</view>
							<view class="cu-form-group">
								<view class="titles">描述：</view>
								<textarea value="" placeholder="请输入描述" style="text-align: left;text-indent: 2em;" v-model="myModelData.affairContent" />
								</view>
							<view class="padding flex flex-direction">
								<button class="cu-btn lg"  @click="onSubmit">确定</button>
							</view>
						</form>
					</view>
				</view>
			</view>
			<!-- 详情 -->
			<view class="list">
				<view class="cu-list menu animation-slide-bottom" v-for="(item,index) in WordListMsg" :key="index" :style="[{animationDelay: (index + 1)*0.1 + 's'}]">
					<view class="cu-item">
						<view class="content">
							<text class="text-grey headLine">料号</text>
						</view>
						<view class="action">
							<text class="text-grey headLine">{{item.materielCode}}</text>
						</view>
					</view>
					<view class="cu-item">
						<view class="content">
							<text class="text-grey">物料名称</text>
						</view>
						<view class="action">
							<text class="text-grey">{{item.materielName}}</text>
						</view>
					</view>
					<view class="cu-item">
						<view class="content">
							<text class="text-grey">提货数量</text>
						</view>
						<view class="action">
							<text class="text-grey">{{item.pickupNum}}</text>
						</view>
					</view>
					<view class="cu-item">
						<view class="content">
							<text class="text-grey">需求数量</text>
						</view>
						<view class="action">
							<text class="text-grey">{{item.requireNum}}</text>
						</view>
					</view>
					<view class="cu-item">
						<view class="numName" style="background-color: #99CC00;">
							发料数量:
						</view>
						<view class="numInput">
							<input style="width: 260rpx;" type="text" v-model="preservationList[index].num" value="" placeholder="请输入发料数量" />
						</view>
					</view>
					<view class="cu-item">
						<view class="btn1" @click="FirstArticleIssue(index)">发料</view>
						<view class="btn2" @click="scanShipment(index)">扫描发料</view>
					</view>
					<view class="line">
						
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		name: 'workOrderMaterialPreparationDetail',
		data() {
			return {
				produceId: '', // 制令单中 生产订单ID
				WordListMsg: [],
				modalName:'',
				myModelData:{
					cerDate:'', //凭证日期
					accountDate:'', // 过账日期
					affairType:'',
					affairContent:''
				},
				// 有一个子订单  次数数组就有几个对象 用于存储id + 发货数量
				preservationList:[],
			}
		},
		onLoad(e) {
			this.produceId = uni.getStorageSync('workOrderMaterialPreparationProduceId');
			this.getProduceMesCommandbillInfoListbom();
			let date = new Date()
			this.myModelData.cerDate = date.getFullYear() + '-' +(date.getMonth() + 1) + '-' + date.getDate();
			this.myModelData.accountDate = date.getFullYear() + '-' +(date.getMonth() + 1) + '-' + date.getDate();
			this.myModelData.affairType = uni.getStorageSync('affairType');
		},
		methods: {
			// 制造中心-获取bom-分页列表查询
			getProduceMesCommandbillInfoListbom() {
				uni.showLoading({
					mask:true,
					title:'加载中.....'
				})
				let data = {
					produceId: this.produceId
				}
				this.$http.get('produce/mesCommandbillInfo/listbom', {
					params: data
				}).then(res => {
					uni.hideLoading()
					if (res.data.success) {
						this.WordListMsg = res.data.result;
						this.preservationList = []
						for (let i = 0; i < this.WordListMsg.length; i++) {
							let data = {
								id: this.WordListMsg[i].id,
								num: ''
							}
							this.preservationList.push(data)
						}
						console.log(this.WordListMsg)
					} else {
						// this.$tip.error(res.data.message)
            this.modal.show({ type:'error', content:res.data.message })
					}
				})
			},
			// 凭证抬头 按钮
			rightClick(){
				this.modalName = 'Modal'
			},
			// 关闭弹窗
			close(){
				this.modalName = '';
				this.myModelData.affairContent = '';
			},
			// 凭证日期
			cerDateChange(e) {
				console.log(e)
				this.myModelData.cerDate = e.detail.value
			},
			accountDateChange(e){
				this.myModelData.accountDate = e.detail.value
			},
			onSubmit(){
				uni.showLoading({
					mask:true,
					title:'添加凭证抬头中.....'
				})
				if(this.myModelData.affairContent.replace(/\s+/g, "") == ''){
					// this.$tip.error('描述不能为空');
					this.modal.show({ type:'error', content:'描述不能为空' })
					uni.hideLoading()
					return false
				}
				this.$http.post('storage/mesCertificatePerk/add',this.myModelData).then( res =>{
					console.log(res)
					uni.hideLoading()
					if(res.data.success){
						uni.setStorageSync('workOrderMaterialPreparationDetailRise',res.data.result.id);
						// this.$tip.success(res.data.message)
            this.modal.show({ type:'success', content:res.data.message })
						this.modalName = '';
						this.myModelData.affairContent = '';
					}else {
						// this.$tip.error(res.data.message)
            this.modal.show({ type:'error', content:res.data.message })
					}
				})
			},
			// 发料
			FirstArticleIssue(index){
				if (this.preservationList[index].num == '' || this.preservationList[index].num == 0) {
					// this.$tip.error('工单备料发料数量不能为空')
          this.modal.show({ type:'error', content:'工单备料发料数量不能为空' })
					return false
				}else if(!uni.getStorageSync('workOrderMaterialPreparationDetailRise')){
					let date = new Date()
					this.myModelData.cerDate = date.getFullYear() + '-' +(date.getMonth() + 1) + '-' + date.getDate();
					this.myModelData.accountDate = date.getFullYear() + '-' +(date.getMonth() + 1) + '-' + date.getDate();
					this.myModelData.affairType = uni.getStorageSync('affairType');
					this.modalName = 'Modal'
					return false
				}
				uni.showModal({
					title: '工单备料发料',
					content: '确认要发料吗?',
					cancelText: '取消',
					confirmText: '确认',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								mask:true,
								title:'发料中.....'
							})
							let data = {
								mobileCode:261,
								mobileType:'工单备料发料',
								perkId:uni.getStorageSync('workOrderMaterialPreparationDetailRise'),
								reserveCode:this.preservationList[index].id,
								inputNum:this.preservationList[index].num
							}
							this.$http.post('storage/mesCertificatePerk/addMesCertificateItem',data).then( res =>{
								uni.hideLoading()
								if(res.data.success){
									// this.$tip.success(res.data.message)
                  this.modal.show({ type:'success', content:res.data.message })
								}else {
									// this.$tip.error(res.data.message)
                  this.modal.show({ type:'error', content:res.data.message })
								}
							})
						}
					}
				})
			},
			// 扫描发料
			scanShipment(index){
				if(!uni.getStorageSync('workOrderMaterialPreparationDetailRise')){
					this.modalName = 'Modal'
					return false
				}
				uni.showModal({
					title: '工单备料扫描发料',
					content: '确认要扫描发料吗?',
					cancelText: '取消',
					confirmText: '确认',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								mask:true,
								title:'扫描发料中.....'
							})
							let data = {
								mobileCode:261,
								mobileType:'工单备料扫描发料',
								perkId:uni.getStorageSync('workOrderMaterialPreparationDetailRise'),
								reserveCode:this.preservationList[index].id,
								inputNum:this.preservationList[index].num
							}
							this.$http.post('storage/mesCertificatePerk/addMesCertificateItem',data).then( res =>{
								console.log(res.data.result.id)
								if(res.data.success){
									uni.setStorageSync('workOrderMaterialPreparationDetailId',res.data.result.id)
									this.$Router.push({
										name: 'workOrderMaterialPreparationDetailSaoMiao'
									})
								}else {
									// this.$tip.error(res.data.message)
                  this.modal.show({ type:'error', content:res.data.message })
								}
							})
						}
					}
				})
			},
		},
	}
</script>

<style lang="less">
	page {
		background-color: white;
	}

	.title {
		font-weight: bold;
		padding: 10rpx;
		border-radius: 10rpx;
		color: white;
	}

	input {
		border: 2rpx solid #F0F0F0;
		height: 60rpx;
		padding: 10rpx;
		border-radius: 10rpx;
		font-size: 20rpx;
	}

	.btn1,
	.btn2 {
		width: 30%;
		color: white;
		border-radius: 30rpx;
		padding: 10rpx 20rpx;
		text-align: center;
	}

	.btn1 {
		background-image: radial-gradient(circle, #3F55E9, #5F71ED, #7D8CF0);
	}

	.btn2 {
		background-image: radial-gradient(circle, #716BA9, #362D85, #57509A);
	}
	.numName{
		font-weight: bold;
		padding: 10rpx;
		border-radius: 10rpx;
		color: white;
	}
	.line {
		width: 750rpx;
		height: 10rpx;
		background: #F3F5F9;
	}
	.headLine{
		font-size: 28rpx;
		    font-family: PingFang SC;
		    font-weight: bold;
		    color: #191919;
		    line-height: 50rpx;
	}
	.cu-btn{
		background-image: radial-gradient(circle, #3F55E9, #5F71ED, #7D8CF0);
		color: white;
	}
</style>
